package com.controller.after;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.common.ajax.AjaxRes;
import com.common.mybatis.Page;
import com.controller.base.BaseController;
import com.entity.Attachment;
import com.entity.Audit;
import com.entity.Product;
import com.entity.UserIndustry;
import com.entity.Users;
import com.service.AttachmentService;
import com.service.AuditService;
import com.service.IndustryService;
import com.service.ProductService;

@Controller
@RequestMapping("/Product/")
public class ProductController extends BaseController<Object>{
	//

	//附件
	@Autowired
	private AttachmentService fjService;
	//产品
	@Autowired
	private ProductService productService;
	//用户行业
	@Autowired
	private IndustryService IndService;
	
	//审核
	@Autowired
	private AuditService auditServide;
	 
	//查询行业
	@RequestMapping("/findIndustry")
	@ResponseBody
	public AjaxRes findIndustry(){
		System.out.println("PaIndList------------------");
		AjaxRes res=new AjaxRes();
		Page<UserIndustry> page=new Page<>();
		Page<UserIndustry> PaIndList= IndService.findByPage(new UserIndustry(), page);
		System.out.println("PaIndList------------------"+PaIndList);
		res.setSucceed(PaIndList,"数据获取成功");
		return res;	
	}
	
	
	//分页
	@RequestMapping("/findByPage")
	@ResponseBody
	public AjaxRes findByPage(Product o,Page<Product> page)throws Exception{
		AjaxRes res=new AjaxRes();
		Page<Product> indPages =productService.findByPage(o, page);
		res.setSucceed(indPages, "数据获取成功");
		return res;
	}
	
	//转到添加页面
	@RequestMapping("/ShowAdd")
	public ModelAndView ShowAdd(){
		ModelAndView mv =  new ModelAndView();
		mv.addObject("state", "1");
		mv.setViewName("pages/Products/Add" );
		return mv;
	}
	
	
	//转到修改页面
	@RequestMapping("/ShowUpdate")
	public ModelAndView ShowUpdate(Product o){

		ModelAndView mv =  new ModelAndView();
		String NewsIdArry[];
		if(o.getProductId().contains(",")){
			NewsIdArry=o.getProductId().split(",");
		}else{
			NewsIdArry=new String[]{o.getProductId()};
		}
		System.out.println(NewsIdArry[0]);
		o.setProductId(NewsIdArry[0]);
		 
		Audit audit=new Audit();
		audit.setProductId(o.getProductId());
		
		List<Audit> auditList=auditServide.find(audit);
		
		if(auditList.size()==0){
			List<Product> ProductList=productService.findOne(o);
			mv.addObject("ProductList", ProductList.get(0));
		}else{
			List<Product> ProductList=productService.find(o);
			mv.addObject("ProductList", ProductList.get(0));
		}
		System.out.println("查询转到修改Product-----"+o);
		mv.addObject("state", "2");
		mv.setViewName("pages/Products/Update");
		return mv;
	}
	
	//修改和添加
	@RequestMapping("/UpdateOrAdd")
	@ResponseBody
	public AjaxRes UpdateOrAdd(Product o,String AttachmentId,String UserIndustryId,Audit audit)throws Exception{
		AjaxRes res=new AjaxRes();
		HttpSession session=getRequest().getSession();
		Users user= (Users)session.getAttribute("User");
		o.setReleasePeople(user.getUsersName());
		o.setUserIndustryId(UserIndustryId);
		System.out.println("Audit----------------"+audit);
		if(o.getProductId()!=null && !o.getProductId().equals("")){
			try {
				productService.updateAll(o,AttachmentId,audit);
				System.out.println("修改成功");
				res.setResMsg("修改成功");
			} catch (Exception e) {
				res.setResMsg("修改失败");
				System.out.println("修改失败");
			}
		}else{
			try {
				productService.insterFj(o, AttachmentId);
				res.setResMsg("添加成功");
			} catch (Exception e) {
				res.setResMsg("添加失败");
			}
		}
		return res;
	}
	
	
	@RequestMapping("/Delel")
	public ModelAndView Delel(Product o,String idList)throws Exception{
		ModelAndView mv =  new ModelAndView();
		if(o.getProductId()!=null && !o.getProductId().equals("")){
			productService.delete(o);
		}
		if(idList!=null){
			List<String>  gameids= java.util.Arrays.asList(idList.split(","));
			List<Product> PrList=new ArrayList<Product>();
			for(int i=0;i<gameids.size();i++){
				Product ne=new Product();
				ne.setProductId(gameids.get(i));
				PrList.add(ne);
			}
			System.out.println("-----------gameids:"+gameids);
			System.out.println("-----------PrList:"+PrList);
			productService.deleteBatch(PrList);
		}
		mv.setViewName("pages/Products/ProductsContent");
		return mv;
	}
	
	
	//查询产品附件转到修改页面
	@RequestMapping("/ShowAtt")
	@ResponseBody
	public AjaxRes ShowAtt(Product o,Page<Attachment> page){
		AjaxRes res=new AjaxRes();
		Page<Attachment> indPages =fjService.findByPageId(new Attachment(), page,"product",o.getProductId());
		res.setSucceed(indPages, "数据获取成功");
		return res;
	}
}
